﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyDuAn.BUS;
using System.Collections;
using QuanLyDuAn.Report;

namespace QuanLyDuAn.GUI
{
    public partial class FrmSearchApplicant : DevComponents.DotNetBar.Office2007Form
    {
        ApplicantController applicant = new ApplicantController();
        DataSet ds;
        DataSet dsDate = new DataSet();
        ArrayList list = new ArrayList();
        DataSet dsStatus = new DataSet();
        public FrmSearchApplicant()
        {
            InitializeComponent();
        }

        private void FrmSearchApplicant_Load(object sender, EventArgs e)
        {
            cmbCriterion.Items.Add("Interviewer Name");
            cmbCriterion.Items.Add("Date");
            cmbCriterion.Items.Add("Status");
            dtInput.Enabled = false;
            rdOpen.Enabled = false;
            rdClose.Enabled = false;
            rdInprogess.Enabled = false;
            txtApplicantName.Enabled = false;
            LoadDataToCollection();
            this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
        public void CriterionChange()
        {
            String criterion = cmbCriterion.SelectedItem.ToString();
            if (criterion.CompareTo("Status") == 0)
            {
                MessageBox.Show("Please Select Status !");
                dtInput.Enabled = false;
                rdOpen.Enabled = true;
                rdClose.Enabled = true;
                rdInprogess.Enabled = true;
                txtApplicantName.Enabled = false;
            }
            else if (criterion.CompareTo("Date") == 0)
            {
                MessageBox.Show("Please Select Date Input!");
                dtInput.Enabled = true;
                rdClose.Enabled = false;
                rdOpen.Enabled = false;
                rdInprogess.Enabled = false;
                txtApplicantName.Enabled = false;
            }
            else
            {
                MessageBox.Show("Please Enter Name of Applicant !");
                dtInput.Enabled = false;
                rdClose.Enabled = false;
                rdOpen.Enabled = false;
                rdInprogess.Enabled = false;
                txtApplicantName.Enabled = true;
            }
        }
        public int ValueCriterrion()
        {
            int selection;
            String criter = cmbCriterion.Text;
            if (criter.CompareTo("") != 0)
            {
            String criterion = cmbCriterion.SelectedItem.ToString();
            if (criterion.CompareTo("Status") == 0)
            {
                selection = 1;
            }
            else if (criterion.CompareTo("Date") == 0)
            {
                selection = 2;
            }
            else
            {
                selection = 3;
            }
            return selection;
            }
            else
            {
                selection = 0;
                MessageBox.Show("Please select Criterion !");
            }
            return selection;
        }
          public String GetStatus()
        {
            String status;
            if (rdOpen.Checked == true)
            {
                status = "Open";
            }
            else if (rdClose.Checked == true)
            {
                status = "Close";
            }
            else
            {
                status = "Inprogess";
            }
            return status;
        }
        public void SearchByName()
        {
            String name = txtApplicantName.Text;
            ds = new DataSet();
            ds = applicant.SearchApplicantByName(name);
            dataGridViewX1.DataSource = ds.Tables[0];
        }

        public void SearchByDate()
        {
            String date = dtInput.Value.ToString();
            dsDate = applicant.SearchApplicantByDate(date);
            dataGridViewX1.DataSource = dsDate.Tables[0];

        }
        public void SearchByStatus()
        {
            String status = this.GetStatus();
            dsStatus = applicant.SearchApplicantByStatus(status);
            dataGridViewX1.DataSource = dsStatus.Tables[0];
        }
        public void LoadDataToCollection()
        {
            AutoCompleteStringCollection autoString = new AutoCompleteStringCollection();
            list = applicant.GetApplicantName();
            foreach (String Name in list)
            {
                autoString.Add(Name);
            }
            txtApplicantName.AutoCompleteMode = AutoCompleteMode.Append;
            txtApplicantName.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtApplicantName.AutoCompleteCustomSource = autoString;
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            int i = this.ValueCriterrion();
            if (i == 3)
            {
                this.SearchByName();
            }
            if (i == 2)
            {
                this.SearchByDate();
            }
            else if (i == 1)
            {
                this.SearchByStatus();
            }
        }

        private void cmbCriterion_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CriterionChange();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            
            
            
            int i = this.ValueCriterrion();
            if (i == 3)
            {
                String applicantName = txtApplicantName.Text;
                FrmApplicantName frmName = new FrmApplicantName();
                frmName.value = applicantName;
                frmName.Visible = true;
                frmName.Show();
            }
            if (i == 2)
            {
                DateTime date = dtInput.Value;
                FrmApplicantDate frmDate = new FrmApplicantDate();
                frmDate.value = date;
                frmDate.Visible = true;
                frmDate.Show();
            }
            else if (i == 1)
            {
                String status = GetStatus();
                FrmApplicantStatus frmStatus = new FrmApplicantStatus();
                frmStatus.value = status;
                frmStatus.Visible = true;
                frmStatus.Show();
            }
        }
    }
}
